const { COZE_BASE_URL, auth } = require('@config/coze/index');
const path = require('path');
const fs = require('fs');
const { getJWTToken } = require('@coze/api');

const homePath = process.cwd();
class CozeService {
    // 用户登录
    static async login(params) {
        const { cozeUserName } = params;
        const keyPath = path.join(homePath, "config/coze/private_key.pem");
        const baseURL = COZE_BASE_URL;
        const appId = auth.oauth_jwt.COZE_APP_ID;
        const aud = auth.oauth_jwt.COZE_AUD;
        const keyid = auth.oauth_jwt.COZE_KEY_ID;
        const privateKey = fs.readFileSync(keyPath).toString();
        const jsonParams = {
            baseURL,
            appId,
            aud,
            keyid,
            privateKey,
            sessionName: cozeUserName, // optional Isolate different sub-resources under the same jwt account
        };
        try {
            const jwtToken = await getJWTToken(jsonParams);
            return [null, jwtToken]
        } catch (err) {
            console.log(err)
            return [err, null]
        }
    }
}

module.exports = {
    CozeService
}
